package com.enfi.api.modules.bz.service.impl;

import com.enfi.api.modules.bz.domain.big.AutomaticData;
import com.enfi.api.modules.bz.service.AutomaticDataService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author wind
 * @date 2020/3/12
 */
@Service
public class AutomaticDataServiceImpl implements AutomaticDataService {
    @Autowired
    private HbaseTemplate hbaseTemplate;

    @Override
    public Map getCharts(List<String> tag_stages, List<String> timeSequence) {
       Map rMap= Maps.newHashMap();
        //存放生成的 rowkey
        tag_stages.forEach(v -> {
            Map map= Maps.newTreeMap();
            String[] arr=v.split("_");
            List<String> l = Lists.newArrayList();
            l.add(arr[0]);
            for (String timeSequ : timeSequence) {
                l.add(arr[0]+ timeSequ);
                map.put(timeSequ,"0");
            }
            if(l==null||l.isEmpty()) {
                return;
            }
            Result[] results = hbaseTemplate.execute(AutomaticData.NAME, (table) -> {
                List<Get> getList = Lists.newArrayList();
                l.forEach(t -> {
                    Get get = new Get(Bytes.toBytes(t));
                    getList.add(get);
                });
                return table.get(getList);
            });
            for (Result result : results) {
                if (!result.isEmpty()) {
                for (Cell cell : result.listCells()) {
                    if (new String(CellUtil.cloneQualifier(cell)).equals(arr[1])) {
                        map.put(new String(CellUtil.cloneRow(cell)).substring(new String(CellUtil.cloneRow(cell)).length()-12),new String(CellUtil.cloneValue(cell)));
                    }
                }
            }
        }
          rMap.put(v,map);
        });
        return rMap;
    }
}
